package com.fxkj.cloud.drms.application.model.dto;

import com.fxkj.cloud.dbs.common.model.aggregation.QuerySortType;
import com.fxkj.cloud.dbs.common.model.aggregation.AggregationType;
import com.fxkj.cloud.dbs.common.model.aggregation.IntervalType;
import com.fxkj.cloud.dbs.common.model.aggregation.QueryType;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;
import java.util.UUID;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/16 14:30
 */
@Schema(name = "TsQueryDTO", description = "查询ts")
public record TsQueryDTO(
        @Schema(name = "queryType", description = "查询类型")
        Integer queryType,
        @Schema(name = "queryKeys", description = "查询字段")
        List<String> queryKeys,
        @Schema(name = "aggregationType", description = "聚合类型")
        Integer aggregationType,
        @Schema(name = "intervalType", description = "聚合间隔")
        Integer intervalType,
        @Schema(name = "intervalValue", description = "聚合间隔值")
        Integer intervalValue,
        @Schema(name = "startTime", description = "查询开始时间戳")
        Long startTime,
        @Schema(name = "endTime", description = "查询结束时间戳")
        Long endTime,
        @Schema(name = "limit", description = "查询条数")
        Integer limit,
        @Schema(name = "offset", description = "查询偏移量")
        Integer offset,
        @Schema(name = "queryId", description = "查询设备id,多个用逗号连接")
        String queryId,
        @Schema(name = "groupKeys", description = "分组字段")
        List<String> groupKeys,
        @Schema(name = "sortKey", description = "排序字段")
        String sortKey,
        @Schema(name = "sort", description = "排序")
        Integer sort
) {
    public TsQueryDTO {
        QueryType queryTypeEnum = QueryType.of(queryType);
        if (null == queryTypeEnum) {
            throw new IllegalArgumentException("查询类型非法");
        }
        if (null == AggregationType.of(aggregationType)
        ) {
            throw new IllegalArgumentException("聚合类型非法");
        }
        if (null != intervalType
                && (null == IntervalType.of(intervalType)
                || null == intervalValue
                || !IntervalType.of(intervalType).isValid(intervalValue))) {
            throw new IllegalArgumentException("聚合间隔非法");
        }
        if (null == startTime || null == endTime) {
            throw new IllegalArgumentException("查询开始时间戳或查询结束时间戳不能为空");
        }
        if (null != sort && null == QuerySortType.of(sort)) {
            throw new IllegalArgumentException("排序非法");
        }
    }
}
